#include "corexmlgenerator.h"
#include <QDomDocument>
#include <QDebug>
#include "constants.h"

extern const QString CORE_VERSION;
extern const QString PROTOCOL_VERSION;

/**
* @brief construct generator with socket
* @param sock QSslSocket to send generated XML to
* @param parent optional QObject parent
*/
CoreXMLGenerator::CoreXMLGenerator(QSslSocket* sock,QObject *parent) :
    QObject(parent)
{
    username = "";
    socket = sock;
}

/**
* @brief generate core- and protocol version packet
*
* <version>
*  <core>0.1</core>
*  <protocol>1</protocol>
* </version
*/
void CoreXMLGenerator::generateProtocolVersion() {
    qDebug() << "generating protocol version";
    QDomDocument d = getDocument();
    d.appendChild(d.createElement("version"));
    d.childNodes().at(1).appendChild(d.createElement("core"));
    d.childNodes().at(1).appendChild(d.createElement("protocol"));
    QDomText cv = d.createTextNode(CORE_VERSION);
    QDomText pv = d.createTextNode(PROTOCOL_VERSION);
    d.childNodes().at(1).childNodes().at(0).appendChild(cv);
    d.childNodes().at(1).childNodes().at(1).appendChild(pv);

    sendDocument(d);
}


/**
* @brief get a prepared QDomDocument with processing instructions
* @return a QDomDocument with processing instructions
*/
QDomDocument CoreXMLGenerator::getDocument()
{
    QDomDocument d;
    d.appendChild(d.createProcessingInstruction("xml", "version=\"1.0\" encoding=\"utf-8\""));

    return d;
}

/**
* @brief
*
*/
void CoreXMLGenerator::generateShowConnectionState(QString jid,QString state)
{
    qDebug() << "generating show connection state";
    QDomDocument d = getDocument();
    d.appendChild(d.createElement("connection"));
    QDomElement se = d.createElement("state");
    se.setAttribute("value",state);
    se.setAttribute("jid",jid);
    se.setAttribute("type","show");
    d.childNodes().at(1).appendChild(se);

    sendDocument(d);
}


/**
* @brief send a QDomDocument to the client
*
* @param d the QDomDocument to send
*/
void CoreXMLGenerator::sendDocument(QDomDocument d)
{
    Q_ASSERT(socket);
    qDebug() << "sending document: "<< d.toString();
    socket->write(d.toString().toUtf8()+"\r\n");
}

/**
* @brief generate authentication succesful notification
*
* <authenticated>$username</authenticated>
*/
void CoreXMLGenerator::generateAuthenticated(QString username)
{
    QDomDocument d = getDocument();
    QDomNode au = d.createElement("authenticated");
    QDomText dt = d.createTextNode(username);
    au.appendChild(dt);
    d.appendChild(au);

    sendDocument(d);
}

/**
* @brief generate authentication error
*
* <autherror>$message</autherror>
*/
void CoreXMLGenerator::generateAuthError()
{
    qDebug() << "generating auth error";
    QDomDocument d = getDocument();
    QDomNode au = d.createElement("autherror");
    au.appendChild(d.createTextNode("Login failed. No match found for username/password"));
    d.appendChild(au);

    sendDocument(d);
}


/*
  Send initial Data to the client after authentication
  This includes the Buddylist with groups and buddies
  aswell as status-information and messages received while detached
  including those that have not been marked read yet (e.g. send to
  connected client that had no user-interaction since.
  */
void CoreXMLGenerator::sendInitData(int uid)
{
    // Send BuddyList
    /*QSqlDatabase dbconn = QSqlDatabase::database("core");
    Q_ASSERT(dbconn.isValid());



    QSslSocket *socket = sockets.value(uid,NULL);
    Q_ASSERT(socket);

    socket->write(buddylist.toUtf8()+"\r\n");
*/
}

/*
void CoreXMLGenerator::statusChanged(QString type,QString text,QString jid,QString resource,int accid,int uid)
{
    this->sockets.value(uid)->write(QString("<xml version='1.0'><status>"
                                                          "<jid>%1</jid>"
                                                          "<text>%2</text>"
                                                          "<type>%3</type>"
                                                          "<resource>%4</resource>"
                                                          "<accid>%5</accid>"
                                                          "</status>")
                                    .arg(jid)
                                    .arg(text)
                                    .arg(type)
                                    .arg(resource)
                                    .arg(accid).toUtf8()+"\r\n");
}
*/
